﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using GeneralLibs;

namespace Lists
{
    public class Store_TaiKhoan: DoiTuong
    {
        #region StoreID property
        public int StoreID
        {
            get
            {
                return (int)_DataRow["StoreID"];
            }
            set
            {
                _DataRow["StoreID"] = value;
            }
        }
        #endregion

        #region TaiKhoanID property
        public Guid TaiKhoanID
        {
            get
            {
                return (Guid)_DataRow["TaiKhoanID"];
            }
            set
            {
                _DataRow["TaiKhoanID"] = value;
            }
        }
        #endregion

        #region KhoID property
        public Guid KhoID
        {
            get
            {
                return (Guid)_DataRow["KhoID"];
            }
            set
            {
                _DataRow["KhoID"] = value;
            }
        }
        #endregion

        public Store_TaiKhoan(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucStore_TaiKhoan : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucStore_TaiKhoan _Instance;
        public static DanhMucStore_TaiKhoan Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucStore_TaiKhoan();
                    //UniqueConstraint uniqueConstraint = new UniqueConstraint(_Instance.DataTable.Columns["Store_TaiKhoanID"]);
                    //_Instance.DataTable.Constraints.Add(uniqueConstraint);
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                    //_Instance.DataView.Sort = "Store_TaiKhoanID";
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "Store_TaiKhoan";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new Store_TaiKhoan((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public Store_TaiKhoan this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new Store_TaiKhoan(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public Store_TaiKhoan NewDoiTuong()
        {
            return new Store_TaiKhoan(DataTable.NewRow());
        }

        public Store_TaiKhoan GetByProperty(string property, object value)
        {
            DataRow[] rows = GetDataRows(property, value);
            if (rows.Length > 0) return new Store_TaiKhoan(rows[0]);
            else return null;
        }

        public Store_TaiKhoan GetByID(Guid objID)
        {
            return GetByProperty("ID", objID);
        }

        public Store_TaiKhoan GetByStore_TaiKhoanID(int StoreID, Guid TaiKhoanID)
        {
            DanhMucStore_TaiKhoan dm = Filter(string.Format("StoreID = {0} AND TaiKhoanID = '{1}'", StoreID, TaiKhoanID));
            if (dm != null && dm.Count > 0) return dm[0];
            else return null;
        }

        public Store_TaiKhoan GetByStore_KhoID(int StoreID, Guid KhoID)
        {
            DanhMucStore_TaiKhoan dm = Filter(string.Format("StoreID = {0} AND KhoID = '{1}'", StoreID, KhoID));
            if (dm != null && dm.Count > 0) return dm[0];
            else return null;
        }

        public static DanhMucStore_TaiKhoan GetAll()
        {
            return Instance;
        }

        public static DanhMucStore_TaiKhoan Filter(string filterExpression)
        {
            try
            {
                DanhMucStore_TaiKhoan obj = new DanhMucStore_TaiKhoan();
                obj.DataView = new DataView(DanhMucStore_TaiKhoan.Instance.DataTable, filterExpression, "", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

    }
}
